﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VSSAHT.DataAccess;
using VSSAHT.Transfer;
namespace VSS_AHT
{
    public partial class FormUpdateCustomer : BaseForm
    {

        //#region Singleton Members
        //private static volatile FormUpdateCustomer instance;
        //private static object syncRoot = new Object();

        //public static FormUpdateCustomer Instance
        //{
        //    get
        //    {
        //        lock (syncRoot)
        //        {
        //            if (instance == null)
        //                instance = new FormUpdateCustomer();
        //        }

        //        return instance;
        //    }
        //}
        //#endregion

        #region Ctors
        public FormUpdateCustomer()
        {
            InitializeComponent();
            Initialize();
        }
        public void Initialize()
        {
            this.MaximizeBox = false;

            this.Load += new EventHandler(delegate
            {
                BindingControls();
                ValidatingControls();

                this.txtCustomerCode.Enabled = !_isEdit;
            });
            this.btnSave.Click += new EventHandler(delegate { ClickButtonSave(); });
            this.btnClose.Click += new EventHandler(delegate { this.Close(); });
        }
        #endregion

        #region Property Member
        private bool _isEdit = false;
        private bool _isBinding = false;
        private CustomerDTO _customer = new CustomerDTO();
        CustomerDataAccess _dataAccess = new CustomerDataAccess();
        public CustomerDTO Customer
        {
            get { return _customer; }
            set
            {
                if (_customer != value)
                {
                    _isEdit = true;
                    _customer = value;
                }
            }
        }

        private IList<CustomerDTO> _collection;
        public IList<CustomerDTO> Collection
        {
            set { _collection = value; }
        }
        #endregion

        #region Methods
        private void BindingControls()
        {
            if (!_isBinding)
            {
                this.txtCustomerCode.DataBindings.Add("Text", Customer, "CustomerCode");
                this.txtCustomerName.DataBindings.Add("Text", Customer, "CustomerName");
                //this.txtItemUnit.DataBindings.Add("Text", Item, "ItemUnit");
                _isBinding = true;
            }
        }

        private void ValidatingControls()
        {
            string errorMessage = String.Empty;
            this.txtCustomerName.Validating += new CancelEventHandler(delegate
            {
                errorMessage = String.IsNullOrEmpty(this.txtCustomerName.Text) ?
                    "Tên khách hàng cần được nhập" : String.Empty;
                errorProvider.SetError(this.txtCustomerName, errorMessage);
            });
        }

        private void ResetBindingControls()
        {
            this.txtCustomerCode.DataBindings.Clear();
            this.txtCustomerName.DataBindings.Clear();
          
            this._isBinding = false;
        }

        private void ClickButtonSave()
        {
            bool invalidInput = false;

            foreach (Control ctrl in this.Controls)
            {
                if (errorProvider.GetError(ctrl) != String.Empty)
                {
                    invalidInput = true;
                    break;
                }
            }

            if (invalidInput)
            {
                Common.ShowAlert("Thông tin bạn nhập chưa hợp lệ, vui lòng kiểm tra lại");
            }
            else
            {
                this.errorProvider.Clear();

                bool result = false;
                if (!_isEdit)
                {
                    result = _dataAccess.Add(_customer);

                    if (result)
                    {
                        _collection.Add(_customer);
                        _customer = new CustomerDTO();
                        ResetBindingControls();
                        BindingControls();
                        this.txtCustomerCode.Focus();
                    }
                    else
                    {
                        Common.ShowAlert("Tạo mới thông tin khách hàng không thành công!");
                    }
                }
                else
                {
                    result = _dataAccess.Edit(_customer);

                    if (result)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        Common.ShowAlert("Chỉnh sửa thông tin khách hàng không thành công!");
                    }

                }
            }
        }

        
        #endregion

        #region Events

        #endregion
    }
}